package com.quanyan.ic.middleware.redis.cluster;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {

    private int maxRedirects;

    private String nodes;

    public List<RedisNode> getRedisNodes() {
        List<RedisNode> redisNodes = new ArrayList<>();
        if (!StringUtils.isEmpty(this.nodes)) {
            String[] hps = this.nodes.split(",");
            for (String hp : hps) {
                if (StringUtils.isEmpty(hp) || "".equals(hp.trim())) {
                    continue;
                }
                String[] _hp = hp.split(":");
                RedisNode redisNode = new RedisNode(_hp[0], Integer.valueOf(_hp[1]));
                redisNodes.add(redisNode);
            }
        }
        return redisNodes;
    }

    public int getMaxRedirects() {
        return maxRedirects;
    }

    public void setMaxRedirects(int maxRedirects) {
        this.maxRedirects = maxRedirects;
    }

    public String getNodes() {
        return nodes;
    }

    public void setNodes(String nodes) {
        this.nodes = nodes;
    }
}
